package com.zhanbo.zhanbowymusic.search;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.utils.KeyboardUtils;
import com.mwx.baselib.view.RecycleViewDivider;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentSearchBinding;
import com.zhanbo.zhanbowymusic.main.featured.FeaturedListFragment;
import com.zhanbo.zhanbowymusic.main.featured.RankingListFragment;
import com.zhanbo.zhanbowymusic.main.featured.SingerListFragment;
import com.zhanbo.zhanbowymusic.main.featured.VipListFragment;
import com.zhanbo.zhanbowymusic.main.featured.adapter.PCZoneListAdapter;
import com.zhanbo.zhanbowymusic.search.adapter.SearchHistoryListAdapter;
import com.zhanbo.zhanbowymusic.search.adapter.SearchHotListAdapter;
import com.zhanbo.zhanbowymusic.search.adapter.SearchPlayListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索页
 */
public class SearchFragment extends BaseFragment<FragmentSearchBinding> implements View.OnClickListener, TextView.OnEditorActionListener {
    private SearchSongListFragment searchSongListFragment;
    private SearchSingerListFragment searchSingerListFragment;
    private SearchPlayListFragment searchPlayListFragment;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_search;
    }
    private String[] mTitlesArrays ={"单曲","歌单","歌手"};
    private ArrayList<Fragment> fragments;

    @Override
    protected void init(Bundle savedInstanceState) {
        initData();
        initView();
        initListener();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        KeyboardUtils.openKeyboard(_mActivity,binding.etEditSearch);
        binding.etEditSearch.requestFocus();
//        List<Integer> stringList = new ArrayList<>();
//        stringList.add(1);
//        stringList.add(1);
//        stringList.add(1);
//        stringList.add(1);
//        stringList.add(1);
//        stringList.add(1);
//        stringList.add(1);
//        binding.rvSearchHistory.setLayoutManager(new GridLayoutManager(_mActivity, 4));
//        binding.rvSearchHistory.addItemDecoration(new RecycleViewDivider(_mActivity, LinearLayoutManager.HORIZONTAL,17,getResources().getColor(R.color.color_F7F9FC)));
//        SearchHistoryListAdapter adapter = new SearchHistoryListAdapter(_mActivity);
//        adapter.getItems().addAll(stringList);
//        binding.rvSearchHistory.setAdapter(adapter);
//
//        List<Integer> stringList1 = new ArrayList<>();
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        stringList1.add(1);
//        binding.rvSearchHot.setLayoutManager(new GridLayoutManager(_mActivity, 2));
//        binding.rvSearchHot.addItemDecoration(new RecycleViewDivider(_mActivity, LinearLayoutManager.HORIZONTAL,22,getResources().getColor(R.color.color_F7F9FC)));
//        SearchHotListAdapter adapter1 = new SearchHotListAdapter(_mActivity);
//        adapter1.getItems().addAll(stringList1);
//        binding.rvSearchHot.setAdapter(adapter1);
         searchSongListFragment = SearchSongListFragment.newInstance();
        searchSingerListFragment = SearchSingerListFragment.newInstance();
        searchPlayListFragment = SearchPlayListFragment.newInstance();

        fragments = new ArrayList<>();
        fragments.add(searchSongListFragment);
        fragments.add(searchPlayListFragment);
        fragments.add(searchSingerListFragment);
        binding.vpFeatured.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        binding.tabFeatured.setViewPager(binding.vpFeatured, mTitlesArrays);
//        binding.tabFeatured.setViewPager(binding.vpFeatured,mTitlesArrays,getActivity(),fragments);
        binding.vpFeatured.setOffscreenPageLimit(3);
    }

    @Override
    protected void initListener() {
        binding.btnBack.setOnClickListener(this);
        binding.etEditSearch.setOnEditorActionListener(this);
        binding.etEditSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    KeyboardUtils.closeKeyboard(getActivity());
                    if (!TextUtils.isEmpty(v.getText().toString().trim())){
                        searchPlayListFragment.getData(v.getText().toString().trim());
                        searchSongListFragment.getData(v.getText().toString().trim());
                        searchSingerListFragment.getData(v.getText().toString().trim());
                    }
                    return true;
                }
                return false;
            }
        });
    }

    public static SearchFragment newInstance() {

        Bundle args = new Bundle();

        SearchFragment fragment = new SearchFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                pop();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if(i == EditorInfo.IME_ACTION_DONE){
            Editable text = binding.etEditSearch.getText();
            if(!TextUtils.isEmpty(text)){
                return true;
            }
        }
        return false;
    }
}
